// pages/mine_center/invite/invite.js
var app = getApp();
var request = require("../../../utils/request.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sun_ma: "http://ou017obwg.bkt.clouddn.com/invite02.png"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var user = wx.getStorageSync("totalInfo");
    this.setData({
      user: user
    })
    var temp;
    wx.downloadFile({
      url: user.headImg,
      success: function (res_data) {
        temp = res_data.tempFilePath;

        request.request(app.globalData.getWXQARCode, {}, function (res) {
          // console.log(res)
          if (res.code == 0) {
            var wxcode = request.url + "/" + res.codeSrc;
            // console.log(wxcode);
            that.setData({
              wxcode: wxcode
            })
            wx.getImageInfo({
              src: wxcode,
              success: (res_er) => {

                wx.getSystemInfo({
                  success: res => {
                    // console.log(res);
                    var windowWidth = res.windowWidth;
                    var windowHeight = res.windowHeight;
                    var unit = windowWidth / 375;
                    var context = wx.createCanvasContext('share');       //打开画布
                    context.setFillStyle('white');
                    context.fillRect(0, 0, windowWidth, windowHeight);
                    context.drawImage(res_data.tempFilePath, 30 * unit, 30 * unit, 50 * unit, 50 * unit);
                    context.drawImage(res_er.path, 70 * unit, 118 * unit, 240 * unit, 240 * unit);
                    context.setFontSize(17);
                    context.setFillStyle("#333");
                    context.fillText(user.username, 100 * unit, 57 * unit);
                    // context.fillText("点击预览保存图片", 130 * unit, 415 * unit);
                    context.drawImage("../../../images/invite03.png", 105 * unit, 410 * unit, 180 * unit, 60 * unit);
                    // context.draw();
                    //把画板内容绘制成图片，并回调 画板图片路径
                    context.draw(false, function () {
                      wx.canvasToTempFilePath({
                        x: 0,
                        y: 0,
                        width: 375 * unit,
                        height: unit * 498,
                        canvasId: 'share',
                        quality: 1,
                        success: function (res) {
                          // console.log(res)
                          that.setData({
                            context: res.tempFilePath
                          })
                        }
                      })
                    });
                  }
                })

              }
            })

          }
        })        
      },
    })





  },
  loadimg:function(){
    var that = this;
    var wxcode = this.data.wxcode;

    wx.getImageInfo({
      src: wxcode,
      success: (res) => {


      }
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  canvas: function () {
    var context = this.data.context;
    wx.previewImage({
      current: context, // 当前显示图片的http链接
      urls: [context], // 需要预览的图片http链接列表
      success: function (res) {
        console.log(res)
        wx.saveImageToPhotosAlbum({
          filePath: context,
          success(data) {
            console.log(data)
            wx.showToast({
              icon: "none",
              title: "已保存到系统相册",
            })
          },
          fail: function (data) {
            console.log(data)
            wx.showToast({
              icon: "none",
              title: "保存失败",
            })
          }
        })
      },
    })
  }
})